﻿using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities.Auditing;

namespace Wy.JiZhangSoft.Dictionaries
{
    /// <summary>
    /// 字典
    /// </summary>
    public class Dictionary:FullAuditedAggregateRoot<Guid>
    {
        public Dictionary(Guid id):base(id)
        { 
            
        }
        protected Dictionary() { }

        public string Code { get; set; }

        public string Name { get; set; }

        public string Value { get; set; }

        public bool IsEnable { get; set; }

        public int SerialNumber { get; set; }

        public void SetCode(string parentCode)
        {
            bool isExis = ParentCodes.Exis(parentCode);
            if (!isExis)
                throw new Exception("ParentCode 不存在");
            if (string.IsNullOrWhiteSpace(Name))
                throw new Exception("Name 不能够为空");
            Code = $"{parentCode}.{GetPinyin(Name)}";
        }

        /// <summary> 
        /// 汉字转化为拼音
        /// </summary> 
        /// <param name="str">汉字</param> 
        /// <returns>全拼</returns> 
        private static string GetPinyin(string str)
        {
            string r = string.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, t.Length - 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }
    }
}
